package com.ruoyi.web.controller.flyflow.util;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * FlyFlow 工作流工具
 *
 * @author Jyl
 */
public class FlyFlowUtil
{

    /**
     * @return 服务地址
     */
    public static String getBaseUrl()
    {
        return SpringUtil.getProperty("flyflow.url");
    }

    /**
     * @param headerParamMap 请求头
     * @param object         请求参数
     * @param url            请求地址
     * @return 回执信息
     */
    public static String post(Map<String, String> headerParamMap, Object object, String url)
    {
        String baseUrl = getBaseUrl();
        HttpResponse httpResponse = HttpRequest.post(baseUrl + url)
                .headerMap(headerParamMap, true)
                .body(JSONObject.toJSONString(object))
                //超时，毫秒
                .timeout(60000)
                .execute();

        return httpResponse.body();
    }

    /**
     * @param headerParamMap 请求头
     * @param url            请求地址
     * @return 回执信息
     */
    public static String get(Map<String, String> headerParamMap, String url)
    {
        String baseUrl = getBaseUrl();
        HttpResponse httpResponse = HttpRequest.get(baseUrl + url)
                .headerMap(headerParamMap, true)
                //超时，毫秒
                .timeout(60000)
                .execute();

        return httpResponse.body();
    }

    /**
     * 获取 流程模板
     *
     * @param json 用户可操作流程集
     * @param name 获取流程名称
     * @return flowId
     */
    public static JSONObject getProcessInstance(JSONObject json, String name)
    {
        JSONArray data = json.getJSONArray("data");
        //遍历分组
        for (Object datum : data)
        {
            JSONObject jsonObject = (JSONObject) datum;
            //匹配分组
            if (!jsonObject.getString("name").equals("审批"))
            {
                continue;
            }
            JSONArray items = jsonObject.getJSONArray("items");
            for (Object item : items)
            {
                JSONObject itemJson = (JSONObject) item;
                //匹配流程
                if (itemJson.getString("name").equals(name))
                {
                    return itemJson;
                }
            }
        }
        return new JSONObject();
    }

    /**
     * 构建 流程启动表单参数
     *
     * @param info   流程单据对象
     * @param entity 业务单据对象
     * @return paramMap
     */
    public static JSONObject buildParamMap(JSONObject info, JSONObject entity)
    {
        JSONObject paramMap = new JSONObject();
        JSONObject data = info.getJSONObject("data");
        JSONArray formList = data.getJSONArray("formList");
        for (Object o : formList)
        {
            JSONObject form = (JSONObject) o;
            if (form.containsKey("placeholder"))
            {
                String key = form.getString("placeholder");
                String id = form.getString("id");
                paramMap.put(id, entity.get(key));
            }
        }
        JSONArray userDetp = data.getJSONArray("startUserDeptList");
        for (Object o : userDetp)
        {
            JSONObject user = (JSONObject) o;
            paramMap.put("startUserMainDeptId", user.getString("id"));
        }
        return paramMap;
    }

    /**
     * 解析 待办数量
     *
     * @param jsonObject 查询返回对象
     * @return total
     */
    public static String analysisTaskTotal(JSONObject jsonObject)
    {
        if (jsonObject.containsKey("ok") && jsonObject.getBoolean("ok"))
        {
            return jsonObject.getJSONObject("data").getString("total");
        }
        return "0";
    }
}
